package com.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmer.pojo.Goods;
import com.farmer.service.GoodsService;
import com.farmer.mapper.GoodsMapper;
import com.farmer.utils.Result;
import com.farmer.utils.ResultCodeEnum;
import com.farmer.utils.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
* @author 86183
* @description 针对表【goods(扶贫商品表)】的数据库操作Service实现
* @createDate 2024-04-28 13:28:04
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService{
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Result add(Goods goods) {
        return Result.ok(null);
    }

    @Override
    public Result delete(Integer id) {
       goodsMapper.deleteById(id);
        return null;
    }
    @Override
    public Result deleteBatch(List<Integer> ids){
        for(Integer id:ids){
            goodsMapper.deleteById(id);
        }
        return Result.ok(null);
    }

    @Override
    public Result update(Goods goods) {
        goodsMapper.updateById(goods);
        return Result.ok(null);
    }


    @Override
    public Result getById(Integer id) {
       Goods goods = goodsMapper.selectById(id);
        if (goods!=null) {
            return Result.build(goods, 111, "查询成功");
        }else{
            return Result.build(null,101,"查询失败");
        }
    }

    @Override
    public Result getAll() {
        List<Goods> goods=goodsMapper.selectList(null);
        if (goods!=null&&!goods.isEmpty()) {
            return Result.build(goods, 111, "查询成功");
        }else{
            return Result.build(null,101,"查询失败");
        }
    }

    @Override
    public Result getByTitleLike(String title){
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
      lambdaQueryWrapper.like(Goods::getName,title);
      List<Goods> goods = goodsMapper.selectList(lambdaQueryWrapper);
      if (goods!=null&&!goods.isEmpty()){
          return Result.build(goods,111,"查询成功");
      }else{
          return Result.build(null,101,"未找到匹配信息");
      }
    }
    @Override
    public Result ImgInsert(Integer id, MultipartFile file, String name, Double price, Integer num, Integer sales) {
        try {
            String url = Upload.uploadImage(file);
            System.out.println("图片的url：" + url);
            // 首先检查ID是否已存在于数据库中
            Goods goods = goodsMapper.selectById(id);
            if (goods == null) {
                // ID不存在，添加新记录
                Goods newGoods = new Goods();
                newGoods.setId(id);
                newGoods.setImg(url);
                newGoods.setName(name);
                newGoods.setPrice(price);
                newGoods.setNum(num);
                newGoods.setSales(sales);
                goodsMapper.insert(newGoods);
                return Result.build(url, ResultCodeEnum.SUCCESS);
            } else {
                // ID存在，更新现有记录的图片URL
                goods.setImg(url);
                goodsMapper.updateById(goods);
                return Result.build(url, ResultCodeEnum.SUCCESS);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return Result.build(null, ResultCodeEnum.FAULT);
        }
//    @Override
//    public Result ImgInsert(Integer id, MultipartFile file) {
//        try {
//            String url = Upload.uploadImage(file);
//            System.out.println("图片的url：" + url);
//            Goods goods = goodsMapper.selectById(id);
//            // 更新数据库中商品的图片
//            goods.setId(id);
//           goods.setImg(url); // 将 OSS 返回的图片URL 存储到数据库中
//            updateById(goods);
//            return Result.build(url, ResultCodeEnum.SUCCESS);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return Result.build(null, ResultCodeEnum.FAULT);
//        }
//    }
    }

    /*
     * 分页查询*/
    @Override
    public Result page(Integer pageNum, Integer pageSize){
        // 创建分页构造对象
        Page<Goods> page = new Page<>(pageNum, pageSize);
        // 执行分页查询
        IPage<Goods> result = goodsMapper.selectPage(page, null);
        // 封装到统一响应体Result中
        return Result.ok(result);
    }

}




